package com.example.tellwin.mine.act;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.tellwin.AppHelper;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.event.ExpenseCalendarEvent;
import com.example.tellwin.event.MineEvent;
import com.example.tellwin.mine.contract.ApplyResultContract;
import com.example.tellwin.mine.contract.WithdrawMoneyContract;
import com.example.tellwin.mine.presenter.WithdrawMoneyPresenter;
import com.example.tellwin.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawMoneyActivity extends CpBaseActivty implements WithdrawMoneyContract.View {

    @BindView(R.id.balance_tv)
    TextView balanceTv;
    @BindView(R.id.explain_tv)
    TextView explainTv;
    @BindView(R.id.money_et)
    EditText moneyEt;
    @BindView(R.id.wechat_rbtn)
    RadioButton wechatRbtn;
    @BindView(R.id.ali_rbtn)
    RadioButton aliRbtn;
    @BindView(R.id.unionpay_rbtn)
    RadioButton unionpayRbtn;
    @BindView(R.id.confrim_btn)
    TextView confrimBtn;

    @Inject
    WithdrawMoneyPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_money);
        ButterKnife.bind(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);
    }

    @Override
    protected void initEvents() {
        moneyEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setEnable();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private void setEnable() {
        if (moneyEt.getText().toString().length() == 0) {
            confrimBtn.setEnabled(false);
            return;
        }
        confrimBtn.setEnabled(true);
    }

    @Override
    protected void initDatas() {
        setTitle("提现");
        balanceTv.setText(AppHelper.getInstance().getUserInfo().getBalance()+"");
        mPresenter.getWithDrawalHint();
    }

    @OnClick(R.id.confrim_btn)
    public void onViewClicked() {
        int type = 1;
        if(wechatRbtn.isChecked()){
            type =1;
        }else if(aliRbtn.isChecked()){
            type = 2;
        }else if(unionpayRbtn.isChecked()){
            type =3 ;
        }
        mPresenter.withDrawal(moneyEt.getText().toString() , type);
    }

    @Override
    public void withDrawalSuccess() {
        ToastUtil.show(this , "请等待审核！");
        finish();
        EventBus.getDefault().post(new ExpenseCalendarEvent());
        EventBus.getDefault().post(new ExpenseCalendarEvent());
    }

    @Override
    public void gwithDrawalHint(String withdrawalExplain) {
        explainTv.setText(withdrawalExplain);
    }
}
